package Graphics;

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;

import util.AStar;
import util.FieldCreator;
import util.Node;
import util.Pathfinder;
import util.PublicVars;

public class SettingsPanel extends JPanel implements ActionListener{
	
	private JPanel panelGridSettings;
	private JLabel labelNodeSize = new JLabel(" Node Size: ");
	private JSpinner spinnerNodeSize = new JSpinner(new SpinnerNumberModel(40,25,60,5));
	private JLabel labelGridSize = new JLabel(" Grid Size: ");
	private JSpinner spinnerGridSize = new JSpinner(new SpinnerNumberModel(25,10,60,5));
	
	private JLabel labelTimeToWait = new JLabel(" Delay: ");
	private JSpinner spinnerTimeToWait = new JSpinner(new SpinnerNumberModel(1,0.1,5,0.1));
	private JButton buttonApplyTimeToWait = new JButton("Apply");


	private JSpinner spinnerMazeBlocks = new JSpinner(new SpinnerNumberModel(30,0,100,5));
	private JLabel labelCreateMaze = new JLabel(" Create Maze: ");
	private String[] mazes = {"Empty", "Random", "Structured"};
	private JComboBox comboBoxMaze;
	private JButton buttonApplyGrid = new JButton("Apply");
	private JButton buttonCancelGrid = new JButton("Cancel");
	
	private JPanel panelToolbox;
	private JLabel labelEditMode = new JLabel(" Mode: ");
	private String[] stringModes = {"Move", "Edit", "Start", "Target"};
	private JComboBox comboBoxModes;
	private JLabel labelZoom = new JLabel(" Zoom: ");
	private JSpinner spinnerZoom = new JSpinner(new SpinnerNumberModel(100,5,200,1));
	
	private JPanel panelStartSettings;
	private String[] alogrythms = {"A*", "", ""};
	private JComboBox comboBoxPathfinder;
	private JButton buttonStart = new JButton("Start");
	private JButton buttonStop = new JButton("Stop");
	private JButton buttonClear = new JButton("Clear");


	
	public SettingsPanel(){
		setLayout(new GridLayout(6, 1));
		setSize(200,600);
		//add(new JLabel("Settings"));
		initGridSettings();
	}

	private void initGridSettings() {
		// 1. Abschnitt
		panelGridSettings = new JPanel();
		panelGridSettings.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		panelGridSettings.setLayout(new GridLayout(4,2));
		panelGridSettings.add(labelGridSize);
		panelGridSettings.add(spinnerGridSize);
		panelGridSettings.add(labelNodeSize);
		panelGridSettings.add(spinnerNodeSize);
		//panelGridSettings.add(labelCreateMaze);
		comboBoxMaze = new JComboBox(mazes);
		panelGridSettings.add(comboBoxMaze);
		panelGridSettings.add(spinnerMazeBlocks);
		panelGridSettings.add(buttonApplyGrid);
		panelGridSettings.add(buttonCancelGrid);
		buttonApplyGrid.setActionCommand("applyGrid");
		buttonApplyGrid.addActionListener(this);
		buttonCancelGrid.setActionCommand("cancelGrid");
		buttonCancelGrid.addActionListener(this);
		
		
		// 2. Abschnitt
		panelToolbox = new JPanel();
		panelToolbox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		panelToolbox.setLayout(new GridLayout(4,2));
		//buttonEditMode.setActionCommand("editMode");
		//buttonEditMode.addActionListener(this);
		panelToolbox.add(labelEditMode);
		comboBoxModes = new JComboBox(stringModes);
		panelToolbox.add(comboBoxModes);
		panelToolbox.add(labelZoom);
		panelToolbox.add(spinnerZoom);
		panelToolbox.add(labelTimeToWait);
		panelToolbox.add(spinnerTimeToWait);
		buttonApplyTimeToWait.setActionCommand("applyTimeToWait");
		buttonApplyTimeToWait.addActionListener(this);
		panelToolbox.add(new JPanel());
		panelToolbox.add(buttonApplyTimeToWait);
		
		
		// 3. Abschnitt
		panelStartSettings = new JPanel();
		panelStartSettings.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		panelStartSettings.setLayout(new GridLayout(4,2));
		comboBoxPathfinder = new JComboBox(alogrythms);
		buttonStart.setActionCommand("start");
		buttonStart.addActionListener(this);
		buttonStop.setActionCommand("stop");
		buttonStop.addActionListener(this);
		buttonClear.setActionCommand("clear");
		buttonClear.addActionListener(this);
		panelStartSettings.add(comboBoxPathfinder);
		panelStartSettings.add(buttonStart);
		panelStartSettings.add(buttonStop);
		panelStartSettings.add(buttonClear);

		//panelGridSettings.add(new JPanel());
		add(panelGridSettings);
		add(panelToolbox);
		add(panelStartSettings);
		
		
		refresh();
	}
	
	public void refresh(){
		
		if(PublicVars.getActiveMode() == 0){
			comboBoxModes.setSelectedIndex(0);
		} else if(PublicVars.getActiveMode() == 1){
			comboBoxModes.setSelectedIndex(1);
		} else if(PublicVars.getActiveMode() == 2){
			comboBoxModes.setSelectedIndex(2);
		} else if(PublicVars.getActiveMode() == 3){
			comboBoxModes.setSelectedIndex(3);
		}
		
		spinnerZoom.setValue(Math.round(100*PublicVars.getZoom()));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().equals("applyGrid")){
			Pathfinder.getMainframe().getScreen().noLoop();
		
			PublicVars.setFieldHeight(Integer.parseInt(spinnerGridSize.getValue().toString()));
			PublicVars.setFieldWidth(Integer.parseInt(spinnerGridSize.getValue().toString()));
			
			if(PublicVars.getaStar()!=null){
				PublicVars.getaStar().getMyThread().stop();
			}
			
			if(comboBoxMaze.getSelectedItem().toString().equals("Empty")){
				PublicVars.setNodeNet(FieldCreator.createEmpty());
			} else if(comboBoxMaze.getSelectedItem().toString().equals("Random")){
				PublicVars.setNodeNet(FieldCreator.createRandomMaze((int)spinnerMazeBlocks.getValue()));
			} else if(comboBoxMaze.getSelectedItem().toString().equals("Structured")){
				PublicVars.setNodeNet(FieldCreator.createLogicMaze((int)spinnerMazeBlocks.getValue()));
			}
			
			PublicVars.setNodeSize(Integer.parseInt(spinnerNodeSize.getValue().toString()));
			
			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			//Pathfinder.getMainframe().getScreen().init();
			Pathfinder.getMainframe().getScreen().loop();
		}else if(e.getActionCommand().equals("cancelGrid")){
			
		}else if(e.getActionCommand().equals("editMode")){
			PublicVars.setActiveMode((1+PublicVars.getActiveMode())%4);
			refresh();
		}else if(e.getActionCommand().equals("applyTimeToWait")){
			PublicVars.setAnimSpeed((int)(1000*(double)spinnerTimeToWait.getValue()));
		}else if(e.getActionCommand().equals("start")){
			boolean hasStart = false;
			boolean hasTarget = false;
			if(PublicVars.getaStar()!=null){
				PublicVars.getaStar().getMyThread().stop();
			}
			FieldCreator.clearNodeNet();
			for (int i = 0; i < PublicVars.getNodeNet().length; i++) {
				for (int j = 0; j < PublicVars.getNodeNet()[0].length; j++) {
					if(PublicVars.getNodeNet()[i][j].isTarget()){
						hasTarget = true;
					} else if(PublicVars.getNodeNet()[i][j].isStart()){
						hasStart = true;
					}
				}
			}
			if(hasTarget && hasStart){
				if(comboBoxPathfinder.getSelectedItem().toString().equals("A*")) {
					System.out.println("starting A*");
					PublicVars.setaStar(new AStar());
					PublicVars.getaStar().getMyThread().start();
				}
			}
		}else if(e.getActionCommand().equals("stop")){
			System.out.println("stop");
			if(PublicVars.getaStar()!=null){
				PublicVars.getaStar().getMyThread().stop();
			}
		}else if(e.getActionCommand().equals("clear")){
			System.out.println("clear");
			if(PublicVars.getaStar()!=null){
				PublicVars.getaStar().getMyThread().stop();
			}
			FieldCreator.clearNodeNet();
		}
		
	}
	
	
	
}
